using UnityEngine;

/// <summary>
/// RectTransform扩展功能
/// </summary>
public static class RectTransformExtension
{
    /// <summary>
    /// 返回屏幕空间Rect
    /// </summary>
    /// <param name="rectTransform"></param>
    /// <returns></returns>
    public static Rect ExGetWorldRect(this RectTransform rectTransform)
    {
        Vector3[] vector3s = new Vector3[4];
        rectTransform.GetWorldCorners(vector3s);
        return new Rect(vector3s[0].x, vector3s[0].y, vector3s[2].x - vector3s[0].x, vector3s[2].y - vector3s[0].y);
    }
    /// <summary>
    /// 返回父亲空间Rect
    /// </summary>
    /// <param name="rectTransform"></param>
    /// <returns></returns>
    public static Rect ExGetLocalRect(this RectTransform rectTransform)
    {
        Rect rect = rectTransform.rect;
        Vector2 vector = rectTransform.offsetMin + Vector2.Scale(rectTransform.pivot, rect.size);
        RectTransform parent = rectTransform.parent as RectTransform;
        if (parent != null)
        {
            vector += Vector2.Scale(rectTransform.anchorMin, parent.rect.size);
        }
        rect.x += vector.x;
        rect.y += vector.y;
        return rect;
    }
    /// <summary>
    /// 转换为锚点大小(根据父节点大小自适应)
    /// </summary>
    /// <param name="rectTransform"></param>
    public static void ExSizeToAnchor(this RectTransform rectTransform)
    {
        if (rectTransform == null) return;
        RectTransform rectTransformParent = rectTransform.parent as RectTransform;
        if (rectTransformParent == null) return;

        Rect rect = rectTransform.rect;
        Vector2 vector = rectTransform.offsetMin + Vector2.Scale(rectTransform.pivot, rect.size);
        vector += Vector2.Scale(rectTransform.anchorMin, rectTransformParent.rect.size);
        rect.x += vector.x;
        rect.y += vector.y;
        Rect rectParent = rectTransformParent.rect;

        rectTransform.anchorMin = new Vector2(rect.xMin / rectParent.width, rect.yMin / rectParent.height);
        rectTransform.anchorMax = new Vector2(rect.xMax / rectParent.width, rect.yMax / rectParent.height);
        rectTransform.anchoredPosition = Vector2.zero;
        rectTransform.sizeDelta = Vector2.zero;
    }
    /// <summary>
    /// 转换为锚点位置(根据父节点大小自适应位置)
    /// </summary>
    /// <param name="rectTransform"></param>
    public static void ExPositionToAnchor(this RectTransform rectTransform)
    {
        if (rectTransform == null) return;
        RectTransform rectTransformParent = rectTransform.parent as RectTransform;
        if (rectTransformParent == null) return;

        Rect rect = rectTransform.rect;
        Vector2 vector = rectTransform.offsetMin + Vector2.Scale(rectTransform.pivot, rect.size);
        vector += Vector2.Scale(rectTransform.anchorMin, rectTransformParent.rect.size);
        rect.x += vector.x;
        rect.y += vector.y;
        Rect rectParent = rectTransformParent.rect;

        Vector2 lp = rect.position + Vector2.Scale(rectTransform.pivot, rect.size);

        rectTransform.anchorMin = new Vector2(lp.x / rectParent.width, lp.y / rectParent.height);
        rectTransform.anchorMax = new Vector2(lp.x / rectParent.width, lp.y / rectParent.height);
        rectTransform.anchoredPosition = Vector2.zero;
        rectTransform.sizeDelta = rect.size;
    }
}